# _*_ coding: utf-8 _*_
from odoo import models, api, fields, _
from odoo.exceptions import Warning
import odoo.addons.decimal_precision as dp


class OLProductProductExtend(models.Model):
    _inherit = "product.product"

    product_purchase_limit_control = fields.Boolean('Product Purchase Limit Control')
    product_purchase_limit = fields.Float('Product Purchase Limit')
    spec = fields.Char('Specification')
    safety_stock_qty = fields.Float('Safety Stock Quantity', digits=dp.get_precision('Product Unit of Measure'))
    maximum_stock_qty = fields.Float('Maximum Stock Quantity', digits=dp.get_precision('Product Unit of Measure'))
    # last_purchase_price = fields.Float('Last Purchase Price Unit', digits=dp.get_precision('Product Price'))
    current_purchase_price = fields.Float('Current Purchase Price Unit', digits=dp.get_precision('Product Price'))

    @api.multi
    def write(self, vals):
        res = super(OLProductProductExtend, self).write(vals)
        for order in self:
            if order.product_purchase_limit_control and order.product_purchase_limit <= 0:
                raise Warning(_("Purchase limit must greater then 0 !"))
        return res

    @api.model
    def create(self, vals):
        product_tmpl_id = vals.get("product_tmpl_id", None)
        if product_tmpl_id:
            product_tmpl = self.env["product.template"].browse(product_tmpl_id)
            vals["product_purchase_limit_control"] = product_tmpl.tmpl_purchase_limit_control
            vals["product_purchase_limit"] = product_tmpl.tmpl_purchase_limit
        res = super(OLProductProductExtend, self).create(vals)
        if self.product_purchase_limit_control and self.product_purchase_limit <= 0:
            raise Warning(_("Purchase limit must greater then 0 !"))
        return res
